/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.farmcode.api.runtime.metadata.tables;

import java.io.Serializable;
import java.util.Comparator;
import java.util.HashMap;

/**
 * @author oiprado
 * Fecha: Junio 25 de 2009
 * Clase que permite almacenar la informacion de las tablas, informacion como
 * nombre de la tabla, tipo(table, view) y atributos de la tabla.
 */
public class DataBaseObject extends HashMap<String, String> implements Serializable, Comparator<DataBaseObject> {

    /**
     *
     */
//    private List<Attribute> attributes = new ArrayList<Attribute>();
    private static final long serialVersionUID = 1L;
    private String id;
    /** Representa si el objecto es usado o no **/
    
    private String used;
    private Boolean use;
    private String className;
    /** Representa a la coleccion de atributos asociado al objeto **/
    private AttributeCollection attributeCollection;
    private ReferenceCollection referenceCollection;
    private ConstraintCollection constraintCollection;

    /** Constructor por defecto **/
    public DataBaseObject() {
        super();
        used = "true";//Por defecto el objeto es usado
        attributeCollection  = new AttributeCollection();
        referenceCollection  = new ReferenceCollection();
        constraintCollection = new ConstraintCollection();
    }

    public DataBaseObject(String name, String type) {
        this();
        super.put("NAME", name);
        super.put("TYPE", type);
    }

    /** Permite adicionar valores a CGTDataBaseObject,
     * siempre la llave es en minuscula
     **/
    @Override
    public String put(String key, String value) {
        return super.put(key.toLowerCase(), value);
    }

    /** Permite retornar el valor correspondiente a la llave siempre la llave se
     * guarda en minusculas
     **/
    @Override
    public String get(Object key) {
        return super.get(((String) key).toLowerCase());
    }

    /**
     * @return the used
     */
    public String getUsed() {
        return used;
    }

    /**
     * @param used the used to set
     */
    public void setUsed(String used) {
        this.used = used;
    }

    /**
     * @return the attributeCollection
     */
    public AttributeCollection getAttributeCollection() {
        return attributeCollection;
    }

    /**
     * @param attributeCollection the attributeCollection to set
     */
    public void setAttributeCollection(AttributeCollection attributeCollection) {
        this.attributeCollection = attributeCollection;
    }

    /**
     * @return the id
     */
    public String getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(String id) {
        this.id = id;
    }

    public int compare(DataBaseObject o1, DataBaseObject o2) {
        return o1.get("name").compareTo(o1.get("name"));
    }

    /**
     * @return the use
     */
    public Boolean getUse() {
        return use;
    }

    /**
     * @param use the use to set
     */
    public void setUse(Boolean use) {
        this.use = use;
    }

    /**
     * @return the referenceCollection
     */
    public ReferenceCollection getReferenceCollection() {
        return referenceCollection;
    }

    /**
     * @param referenceCollection the referenceCollection to set
     */
    public void setReferenceCollection(ReferenceCollection referenceCollection) {
        this.referenceCollection = referenceCollection;
    }

  /**
   * @return the constraintCollection
   */
  public ConstraintCollection getConstraintCollection() {
    return constraintCollection;
  }

  /**
   * @param constraintCollection the constraintCollection to set
   */
  public void setConstraintCollection(ConstraintCollection constraintCollection) {
    this.constraintCollection = constraintCollection;
  }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder("<attributes>");
        for (Attribute attribute : attributeCollection) {
            sb.append(attribute.toString());
        }
        sb.append("</attributes>");
        return sb.toString();
    }

    /**
     * @return the className
     */
    public String getClassName() {
        return className;
    }

    /**
     * @param className the className to set
     */
    public void setClassName(String className) {
        this.className = className;
    }
  
}
